<?php
// Supplier SDH
class SDH extends AppModel {
    var $name = 'SDH';

    var $useTable = 'supplier_dispatch_heads';

    var $order = 'SDH.id DESC';

    const DISPATCH_PREFIX = 'SUPPLIER';

    var $belongsTo = array(
            'DispatchStatus' => array(
                'fields' => array('id', 'title')
        ),
            'Customer' => array(
                'fields' => array('id', 'cust_no', 'name')
        ),
            'Site' => array(
                'fields' => array('id', 'site_no', 'name')
        ),
            'Supplier',
            'User' => array(
                'foreignKey' => 'collector_id',
                'fields' => array('username', 'name')
        )
    );

    var $displayField = 'dispatch_no';

    var $hasMany = array(
        'SD' => array('dependent' => true)
    );

    var $validate = array(
            'dispatch_date' => array(
                    'rule' => array('date', 'dmy'),
                    'message' => 'DispatchC date is required.',
                    'required' => true,
        ),
            'delivered_to' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Please input delivered to field.',
                    'required' => true,
        ),
            'customer_id' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Customer is required.',
                    'required' => true,
        ),
            'site_id' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Site is required.',
                    'required' => true,
        ),
            'to_warehouse' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Pliease input to warehouse.',
                    'required' => true,
        ),
    );

    function beforeSave() {
        // Insert
        if (isset($this->data['SDH']['dispatch_date'])) {
            $this->data['SDH']['dispatch_date'] = $this->dateFormatBeforeSave($this->data['SDH']['dispatch_date']);
        }
        return true;
    }

    function afterFind($results) {
        foreach ($results as &$dispatch) {
            if (isset($dispatch['SDH']['dispatch_date'])) {
                $dispatch['SDH']['dispatch_date'] = $this->dateFormatAfterFind($dispatch['SDH']['dispatch_date']);
            }
        }
        return $results;
    }
}
?>
